import java.awt.Color;



public class Game {

	public static final int GAME_SIZE = 10;
	
	protected Player player1 = null;
	protected Player player2 = null;
	
	protected Player currentPlayer = null;
	
	public Game(Player player1, Player player2) {

		//this.area1 = area1;
		//this.area2 = area2;
		this.player1 = player1;
		this.player2 = player2;
		currentPlayer = player2;
		nextPlayer();
	}
	
	public void nextPlayer() {
		if (currentPlayer.equals(player1)) {
			currentPlayer = player2;
		} else {
			currentPlayer = player1;
		}
		player1.textArea.setBackground(Color.WHITE);
		player2.textArea.setBackground(Color.WHITE);
		currentPlayer.textArea.setBackground(Color.LIGHT_GRAY);
	}
	
	public void init() {
		Board clearBoard = new Board(GAME_SIZE);
		player1.textArea.setText(player1.toString() + "\n" + clearBoard.toString() + "\n" + clearBoard.toString());
		player2.textArea.setText(player2.toString() + "\n" + clearBoard.toString() + "\n" + clearBoard.toString());
	}

	public void printBoards() {
		player1.textArea.setText(player1.toString() + "\n" + player1.myBoard.toString() + "\n" + player1.opBoard.toString());
		player2.textArea.setText(player2.toString() + "\n" + player2.myBoard.toString() + "\n" + player2.opBoard.toString());
		if (player1.playerWon)
			player1.textArea.append("WON!!!\n");
		if (player2.playerWon)
			player2.textArea.append("WON!!!\n");
	}
	
	public void begin() {
		player1.putShipsRandomly();
		player2.putShipsRandomly();
		printBoards();
		player1.setOpponent(player2);
		player2.setOpponent(player1);
	}
	
	public Player play() {

		int result = 0;
		result = currentPlayer.strike();
		
		if (result == Player.WON) {
			currentPlayer.playerWon = true;
			printBoards();
			return currentPlayer;
		} else if (result == Player.EMPTY) {
			printBoards();
			nextPlayer();
			return null;
		} else if (result == Player.HIT) {
			printBoards();
			// once again
			return null;
		}
		return null;
	}
}
